package com.app.service;

import com.app.model.cache.RedisKeys;
import com.app.model.main.Approval;
import com.app.model.response.RetCode;
import com.app.model.response.RetStatus;
import com.app.repository.interfaces.ApprovalMapper;
import com.app.service.push.PushService;
import com.app.support.utils.RedisUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/4/7. It's a beautiful day.
 */
@Service
public class ApprovalService {

    @Autowired
    private ApprovalMapper approvalMapper;
    @Autowired
    private ResponseService responseService;
    @Autowired
    private PushService pushService;
    @Autowired
    private RedisUtil redisUtil;

    public RetStatus doApproval( String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap = null;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int type = (int)paraMap.get("type");
        int objectId = (int)paraMap.get("objectId");
        int userId = (int)paraMap.get("userId");
        String time = (String)paraMap.get("time");

        Approval approval = new Approval();
        approval.setObjectId((long)objectId)
                .setUserId((long)userId)
                .setType((byte)type)
                .setTime(time)
                .setFlag((byte)1);

        Approval tApproval = approvalMapper.getFlagWithParam((long)userId,(long)objectId,(byte)type);
        if(tApproval == null){
            if(approvalMapper.insert(approval) == 1){
                //点赞操作的通知
                pushService.pushNotification(approval);

                //在缓存中将帖子的点赞数+1
                String approvalKey = RedisKeys.APPROVAL_NUMBER.ARTICLE + objectId;
                long appvoralNum = 0;
                if(redisUtil.exists(approvalKey)){
                    appvoralNum = ((Integer)redisUtil.get(approvalKey)).longValue();
                }
                redisUtil.set(approvalKey,appvoralNum + 1);

                return responseService.doResponse(100);
            }else{
                return responseService.doResponse(200);
            }
        }else {
            tApproval.setFlag((byte)1);
            if(approvalMapper.updateByPrimaryKeySelective(tApproval) == 1){

                String approvalKey = RedisKeys.APPROVAL_NUMBER.ARTICLE + objectId;
                long appvoralNum = 0;
                if(redisUtil.exists(approvalKey)){
                    appvoralNum = ((Integer)redisUtil.get(approvalKey)).longValue();
                }
                redisUtil.set(approvalKey,appvoralNum + 1);

                return responseService.doResponse(100);
            }else {
                return responseService.doResponse(200);
            }
        }


    }

    public RetStatus getApproval(String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap = null;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int type = (int)paraMap.get("type");
        int objectId = (int)paraMap.get("objectId");
        int userId = (int)paraMap.get("userId");

        Approval approval = approvalMapper.getFlagWithParam((long) userId, (long) objectId, (byte) type);

        if(approval != null){
            byte flag = approval.getFlag();

            Map<String,Integer> map = new HashMap<>();
            map.put("flag", (int) flag);

            RetStatus retStatus = new RetStatus();
            retStatus.setCode(100)
                    .setMessage(RetCode.getMessage(100))
                    .setBody(map);

            return retStatus;
        }else {
            return responseService.doResponse(200);
        }

    }

    public RetStatus cancelApproval(String parameter){
        ObjectMapper objectMapper = new ObjectMapper();
        Map paraMap = null;
        try{
            paraMap = objectMapper.readValue(parameter,Map.class);
        }catch (IOException e){
            e.printStackTrace();
            return responseService.doResponse(204);
        }

        int type = (int)paraMap.get("type");
        int objectId = (int)paraMap.get("objectId");
        int userId = (int)paraMap.get("userId");

        Approval approval = approvalMapper.getFlagWithParam((long)userId,(long)objectId,(byte)type);

        if(approval != null){
            approval.setFlag((byte)0);
            if(approvalMapper.updateByPrimaryKeySelective(approval) == 1){

                String approvalKey = RedisKeys.APPROVAL_NUMBER.ARTICLE + objectId;
                long approvalNum = 0;
                if(redisUtil.exists(approvalKey)){
                    approvalNum = ((Integer)redisUtil.get(approvalKey)).longValue();
                }
                redisUtil.set(approvalKey,approvalNum - 1);

                return responseService.doResponse(100);
            }
        }

        return responseService.doResponse(200);

    }


    public int getApprovalTimesByDay(int days,long userId){
        long time2 = System.currentTimeMillis();
        long time1 = time2 - 24 * 60 * 60 * 1000 * days;

        List<Approval> approvalList = approvalMapper.getListByUserIdBetween(userId,Long.toString(time1),Long.toString(time2));

        if(approvalList == null){
            return 0;
        }else {
            return approvalList.size();
        }

    }

}
